﻿using Common.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;

namespace Common.ViewModels
{
    public class ReflexBuildPage
    {
        public static (FrameworkElement Panel, string Title) BuildPage<T>(T t)
        {
            var xmlCommentHelper = new XmlCommentHelper();
            Type type = t.GetType();
            var assemblyName = type.Assembly.ManifestModule.Name;
            string xml = $"{assemblyName.Substring(0, assemblyName.Length - 4)}.xml";
            xmlCommentHelper.Load(xml);
            string title = xmlCommentHelper.GetTypeComment(type);
            if (string.IsNullOrEmpty(title))
            {
                title = type.Name;
            }

            ScrollViewer scrollViewer = new ScrollViewer() { VerticalScrollBarVisibility = ScrollBarVisibility.Auto };

            UniformGrid uniformGrid = new UniformGrid();
            scrollViewer.Content = uniformGrid;
            uniformGrid.Columns = 4;
            uniformGrid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            uniformGrid.DataContext = t;
            ReflexBuildControl.AddBase<T>(uniformGrid, t);
            TextBlock textBlock = new TextBlock();
            textBlock.Background = new SolidColorBrush(Colors.Red);
            textBlock.Text = "后面为命令";
            uniformGrid.Children.Add(textBlock);
            ReflexBuildControl.AddCommand<T>(uniformGrid, t);

            return (scrollViewer, title);
        }

        public static (FrameworkElement Panel, string Title) BuildPage(object t, Type type)
        {
            var xmlCommentHelper = new XmlCommentHelper();
            var assemblyName = type.Assembly.ManifestModule.Name;
            string xml = $"{assemblyName.Substring(0, assemblyName.Length - 4)}.xml";
            xmlCommentHelper.Load(xml);
            string title = xmlCommentHelper.GetTypeComment(type);
            if (string.IsNullOrEmpty(title))
            {
                title = type.Name;
            }
            ScrollViewer scrollViewer = new ScrollViewer() { VerticalScrollBarVisibility = ScrollBarVisibility.Auto };
            UniformGrid uniformGrid = new UniformGrid();
            scrollViewer.Content = uniformGrid;
            uniformGrid.Columns = 4;
            uniformGrid.VerticalAlignment = System.Windows.VerticalAlignment.Top;

            uniformGrid.Children.Clear();
            uniformGrid.DataContext = t;
            ReflexBuildControl.AddBase(uniformGrid, t, type, xmlCommentHelper);
            TextBlock textBlock = new TextBlock();
            textBlock.Background = new SolidColorBrush(Colors.Red);
            textBlock.Text = "后面为命令";
            uniformGrid.Children.Add(textBlock);
            ReflexBuildControl.AddCommand(uniformGrid, t, type, xmlCommentHelper);
            return (scrollViewer, title);
        }

        public static void BuildVMButton(List<Assembly> assemblies, UniformGrid uniformGrid, TabControl tabControl)
        {
            Type typeINotifyPropertyChanged = typeof(INotifyPropertyChanged);
            List<Type> lisTypes = new List<Type>();
            foreach (Assembly assembly in assemblies)
            {
                var xmlCommentHelper = new XmlCommentHelper();
                var assemblyName = assembly.ManifestModule.Name;
                string xml = $"{assemblyName.Substring(0, assemblyName.Length - 4)}.xml";
                xmlCommentHelper.Load(xml);
                Type[] types = assembly.GetTypes();
                foreach (Type type in types)
                {
                    if (!typeINotifyPropertyChanged.IsAssignableFrom(type))
                    {
                        continue;
                    }
                    BuildVMButton(type, xmlCommentHelper);
                }
            }

            void BuildVMButton(Type type, XmlCommentHelper xmlCommentHelper)
            {
                PropertyInfo? propertyInfo = type.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static);
                object t = null;
                if (propertyInfo != null)
                {
                    t = propertyInfo.GetValue(null);
                }
                else
                {
                    t = Activator.CreateInstance(type);
                }
                TabItem tabItem = new TabItem();

                //关闭


                Button btnOpen = new Button();
                btnOpen.Content = type.Name + "打开";
                btnOpen.Click += (sender, e) =>
                {
                    var panelInfo = ReflexBuildPage.BuildPage(t, type);
                    StackPanel stackPanel = new StackPanel() { Orientation = Orientation.Horizontal };
                    TextBlock textBlock = new TextBlock() { Text = panelInfo.Title };
                    Button btnClose = new Button();
                    btnClose.Content = "关闭";
                    btnClose.Click += (sender, e) =>
                    {
                        if (tabControl.Items.Contains(tabItem))
                        {
                            tabControl.Items.Remove(tabItem);
                        }

                    };
                    stackPanel.Children.Add(textBlock);
                    stackPanel.Children.Add(btnClose);
                    tabItem.Header = stackPanel;
                    tabItem.Content = panelInfo.Panel;
                    if (!tabControl.Items.Contains(tabItem))
                    {
                        tabControl.Items.Add(tabItem);
                    }
                };
                uniformGrid.Children.Add(btnOpen);


            }


        }

        private static void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            //加载状态和命令
            Button button = sender as Button;
            UniformGrid uniformGrid = button.Tag as UniformGrid;

        }
    }
}
